﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace WirtualneNauczanie.Web
{
    public partial class Course : System.Web.UI.Page
    {
        String yourcourseid;
        DropDownList DropDownList1, DropDownList2;
        protected void Page_Load(object sender, EventArgs e)
        {                           
                //pobranie ID kursu ktory ma być wyświetlony
                yourcourseid = Request.QueryString["yourcourseid"];

                //Ustawienie tytułu strony
                kw4();
                /*SqlDataSource1.SelectCommand = "SELECT CourseName FROM Courses WHERE Courses.CourseID =" + yourcourseid;

                DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                String titleString = (String)dv.Table.Rows[0][0];
                TitleLabel.Text = titleString;*/

                //Pobranie nazw lekcji do dropDOwnList1
                kw5();
                /*SqlDataSource1.SelectCommand =
                    "SELECT CourseSectionID, SectionName FROM CourseSections WHERE CourseID =" + yourcourseid + " ORDER BY CourseSectionID";
                
                DropDownList1 = new DropDownList();
                DropDownList1.DataSource = SqlDataSource1;
                DropDownList1.DataTextField = "SectionName";
                DropDownList1.DataValueField = "CourseSectionID";
                DropDownList1.DataBind();
                PlaceHolder1.Controls.Add(DropDownList1);

                //Pobranie nazw testow do dropDOwnList2
                DropDownList2 = new DropDownList();
                DropDownList2.DataSource = SqlDataSource1;
                DropDownList2.DataTextField = "SectionName";
                DropDownList2.DataValueField = "CourseSectionID";
                DropDownList2.DataBind();
                PlaceHolder2.Controls.Add(DropDownList2);*/

                //Pobranie ID usera
                /*MembershipUser myObject = Membership.GetUser();
                string StudentID = myObject.ProviderUserKey.ToString();*/

               /* SqlDataSource1.SelectCommand = "SELECT QuizResults.Score, QuizResults.TotalQuestions, QuizResults.DateTaken, CourseSections.SectionName FROM QuizResults INNER JOIN CourseSections ON QuizResults.CourseSectionID = CourseSections.CourseSectionID INNER JOIN Courses ON CourseSections.CourseID = Courses.CourseID WHERE (Courses.CourseID = " + yourcourseid + ") AND (QuizResults.StudentID = '" + StudentID + "')";
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();*/
                kw6();
                                   
        }
        //Przejscie do nauczania danego kursu
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Pobranie id sekcji kursu
            String section = DropDownList1.SelectedValue;            
            Response.Redirect("CourseText.aspx?yourcourseid=" + yourcourseid + "&section=" + section);

        }
        //Przejscie do testu danego kursu
        protected void Button2_Click(object sender, EventArgs e)
        {
            String section = DropDownList2.SelectedValue;
            Response.Redirect("CourseTest.aspx?yourcourseid=" + yourcourseid + "&section=" + section);
        }

        private void kw4()
        {

            //"SELECT CourseName FROM Courses WHERE Courses.CourseID =" + yourcourseid;
            int yourcourseid = 1; //to bedzie pobierane z http GET
            DataClasses1DataContext db = new DataClasses1DataContext();
            var query = from c in db.Courses
                        where c.CourseID.Equals(yourcourseid)
                        select new
                        {
                            CourseName = c.CourseName,
                        };
            foreach (var course in query)
            {
                TitleLabel.Text = course.CourseName;
            }
        }

        private void kw5()
        {
            //"SELECT CourseSectionID, SectionName FROM CourseSections 
            //WHERE CourseID =" + yourcourseid + " ORDER BY CourseSectionID";
            //int yourcourseid = 1;
            DataClasses1DataContext db = new DataClasses1DataContext();
            var query = from cs in db.CourseSections
                        where cs.CourseID.Equals(yourcourseid)
                        select new
                        {
                            CourseSectionID = cs.CourseSectionID,
                            SectionName = cs.SectionName
                        };
            DropDownList1 = new DropDownList();
            DropDownList2 = new DropDownList();

            foreach (var section in query)
            {
                String id = Convert.ToString(section.CourseSectionID);
                String name = section.SectionName;
                DropDownList1.Items.Add(new ListItem(name, id));
                DropDownList2.Items.Add(new ListItem(name, id));

            }
            PlaceHolder1.Controls.Add(DropDownList1);
            PlaceHolder2.Controls.Add(DropDownList2);
        }


        private void kw6()
        {

            //utworzenie kolumn w tabeli
            //GridView1.Columns.Add("kol1");




            //"SELECT QuizResults.Score, QuizResults.TotalQuestions, QuizResults.DateTaken, CourseSections.SectionName
            //FROM QuizResults INNER JOIN CourseSections ON QuizResults.CourseSectionID = CourseSections.CourseSectionID 
            //INNER JOIN Courses ON CourseSections.CourseID = Courses.CourseID WHERE (Courses.CourseID = " + yourcourseid + ") 
            //AND (QuizResults.StudentID = '" + StudentID + "')";

            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            DataClasses1DataContext db = new DataClasses1DataContext();

            
            var query = from qr in db.QuizResults
                        join cs in db.CourseSections on qr.CourseSectionID equals cs.CourseSectionID
                        join c in db.Courses on cs.CourseID equals c.CourseID
                        where c.CourseID.Equals(yourcourseid) && qr.StudentID.Equals(StudentID)
                        select new
                        {
                            Score = qr.Score,
                            TotalQuestions = qr.TotalQuestions,
                            DateTaken = qr.DateTaken,
                            SectionName = cs.SectionName,
                        };

            GridView1.DataSource = query;
            GridView1.DataBind();

        }
    }
}
